#include "student_info.h"
// 创建学生节点
Student* createStudent(const char *id, const char *name, const char *class_name) {
    Student *newStu = (Student*)malloc(sizeof(Student));
    if (newStu == NULL) {
        printf("内存分配失败！\n");
        return NULL;
    }
    strcpy(newStu->id, id);
    strcpy(newStu->name, name);
    strcpy(newStu->class_name, class_name);
    newStu->next = NULL;
    return newStu;
}

//  添加学生到链表
int addStudent(Student **head, Student *newStu) {
    // 检查学号是否已存在
    Student *current = *head;
    while (current != NULL) {
        if (strcmp(current->id, newStu->id) == 0) {
            printf("错误：学号 %s 已存在！\n", newStu->id);
            return -1; // 学号重复，添加失败
        }
        current = current->next;
    }

    // 添加新节点
    newStu->next = *head;
    *head = (Student*)newStu;
    return 0; // 添加成功
}

// 按学号查找学生
Student* findStudentById(Student *head, const char *id){
    Student *current = head;
    while (current != NULL) {
        if (strcmp(current->id, id) == 0) {
            return current; // 找到，返回节点指针
        }
        current = current->next;
    }
    return NULL; // 未找到
}

// 更新学生信息（按学号）
int updateStudent(Student *head,const  char *id,const  char *newName,const char *newClass){
    Student *stu = findStudentById(head, id);
    if (stu == NULL) {
        printf("错误：学号 %s 不存在！\n", id);
        return -1;
    }
    // 更新姓名和班级
    strcpy(stu->name, newName);
    strcpy(stu->class_name, newClass);
    return 0; // 更新成功
}

//  删除学生信息（按学号）
int deleteStudent(Student **head,const char *id){
    Student *current = *head;
    Student *prev = NULL;

    while (current != NULL) {
        if (strcmp(current->id, id) == 0) {
            if (prev == NULL) {
                // 删除头节点
                *head = current->next;
            } else {
                // 删除中间/尾节点
                prev->next = current->next;
            }
            free(current); // 释放内存
            return 0; // 删除成功
        }
        prev = current;
        current = current->next;
    }
    printf("错误：学号 %s 不存在！\n", id);
    return -1; // 删除失败
}

// 保存学生数据到文件
void saveStudents(Student *head, const char *filename) {
    FILE *fp = fopen(filename, "w");
    if (fp == NULL) {
        printf("错误：无法打开文件 %s\n", filename);
        return;
    }

    Student *current = head;
    while (current != NULL) {
        fprintf(fp, "%s:%s:%s\n", current->id, current->name, current->class_name);
        current = current->next;
    }
    fclose(fp);
    printf("学生数据已保存至 %s\n", filename);
}

//  从文件加载学生数据
void loadStudents(Student **head,const char *filename) {
    FILE *fp = fopen(filename, "r");
    if (fp == NULL) {
        printf("提示：文件 %s 不存在，首次运行将创建新文件\n", filename);
        return;
    }

    *head = NULL; // 清空链表
    char line[200];
    while (fgets(line, sizeof(line), fp) != NULL) {
        // 去除换行符
        line[strcspn(line, "\n")] = '\0';

        char id[MAX_ID], name[MAX_NAME], class_name[MAX_CLASS];
        sscanf(line, "%[^:]:%[^:]:%s", id, name, class_name);

        // 创建学生节点并添加到链表
        Student *newStu = createStudent(id, name, class_name);
        if (newStu != NULL) {
            newStu->next = *head;
            *head = newStu;
        }
    }
    fclose(fp);
    printf("学生数据加载成功\n");
}

// 打印所有学生信息
void printAllStudents(Student *head) {
    if (head == NULL) {
        printf("当前无学生记录\n");
        return;
    }
    printf("=== 学生列表 ===\n");
    Student *current = head;
    while (current != NULL) {
        printf("学号：%s，姓名：%s，班级：%s\n",
               current->id, current->name, current->class_name);
        current = current->next;
    }
}